<?php
//转换是可以的，就是耗时太长了...考虑做成分段的
class pUpgradeAction extends Action{
    
    public function _initialize(){
        set_time_limit(0);
    }
    
    public function index(){
        $this->display();
    }

    public function index2(){
        set_time_limit(0);

        header("content-type:text/html;charset=utf8");

        echo 'upgrade start...<br/>';



        //category
        $categoryQuery = mysql_query("select id,pid,typename,typeformat,jumpurl from pe_category order by level",$link);

        $ids = array();
        $counts = array();  //栏目使用次数计数器

        $dao = null;

        if(false !== $categoryQuery){
            $dao = new CategoryModel();
            $ids['p0'] = 1;     //父节点值为0对应1
            while( $row = mysql_fetch_array($categoryQuery) ){
                $parent = $dao->where('id='.$ids['p'.$row['pid']])->find();
                $id = $row['id'];
                $ids["p{$id}"] = $dao->insertNode(
                array('name'=>$row['typename'],'jump'=>$row['jumpurl']),
                $parent
                );
                $counts["p{$id}"] = 0;
            }

            echo 'category ok<br/>';

            //文章


            $archiveQuery = mysql_query("select id,content,category,title,author,source,summary,keywords,pubdate as created,click as viewnum,thumb from pe_archive p left join pe_archive_content ac on ac.archive = p.id order by id asc",$link);
            if(false !== $archiveQuery){
                $dao1 = new PostModel();
                $dao2 = new One2manyModel();
                while( $row = mysql_fetch_array($archiveQuery) ){
                    $dao1->add($row);
                    $categoryId = $ids["p{$row['category']}"];
                    $dao2->add(array(
                        'one' => $row['id'],
                        'many' => $categoryId,
                        'lft' => 'post',
                        'rgt' => 'category'
                        ));
                        $counts['p'.$categoryId]++;
                }
            }


            foreach($ids as $id){
                $key = "p".$id;
                $dao->where("id={$ids[$key]}")->save(array('total'=>$counts[$key]));
            }

        }

        mysql_close($link);
        echo '<hr/>ok';

    }

    
    public function upgradeCategory(){
        $link = $this->getConnect();
        $query = mysql_query("select id,pid,typename,typeformat,jumpurl from pe_category order by level",$link);
        if(false !== $query){
            $ids = array();
            $counts = array();
            $dao = new CategoryModel();
            $ids['p0'] = 1;     //父节点值为0对应1
            while( $row = mysql_fetch_array($query) ){
                $parent = $dao->where('id='.$ids['p'.$row['pid']])->find();
                $id = $row['id'];
                $ids["p{$id}"] = $dao->insertNode(
                array('name'=>$row['typename'],'jump'=>$row['jumpurl']),
                $parent
                );
                $counts["p{$id}"] = 0;
            }
            F('upgrade_ids',$ids);
            F('upgrade_counts',$counts);
            $this->ajaxReturn(null,null,true);
        }
        $this->ajaxReturn(null,null,false);
    }

    public function upgradePost(){
        $link = $this->getConnect();
        $len = 50;
        $start = intval($_GET['page']) * $len;
        //每次转化50条记录
        $archiveQuery = mysql_query("select id,content,category,title,author,source,summary,keywords,
                    pubdate as created,click as viewnum,thumb 
                    from pe_archive p left join pe_archive_content ac on ac.archive = p.id 
                    order by id asc limit {$start},{$len}",$link);
        if(false !== $archiveQuery){
            $dao1 = new PostModel();
            $dao2 = new One2manyModel();

            $ids = F('upgrade_ids');
            $counts = F('upgrade_counts');

            $c = 0;
            while( $row = mysql_fetch_array($archiveQuery) ){
                $c++;
                $dao1->add($row);
                $categoryId = $ids["p{$row['category']}"];
                $dao2->add(array(
                        'one' => $row['id'],
                        'many' => $categoryId,
                        'lft' => 'post',
                        'rgt' => 'category'
                        ));
                        $counts['p'.$categoryId]++;
            }
            if($c == 0){
                //说明全部文章已经转化完毕
                $dao = new CategoryModel();
                foreach($ids as $id){
                    $key = "p".$id;
                    $dao->where("id={$ids[$key]}")->save(array('total'=>$counts[$key]));
                }
                F('upgrade_ids',null);
                F('upgrade_counts',null);
                $this->ajaxReturn(null,'ok',true);
            }else{
                F('upgrade_ids',$ids);
                F('upgrade_counts',$counts);
                $this->ajaxReturn(null,'nextpage',true);
            }
        }
        $this->ajaxReturn(null,null,false);
    }



    private function getConnect(){
        $link = mysql_connect(C('db_host'),C('db_user'),C('db_pwd'),true);
        if(false === $link){
            exit('connect database fail');
        }
        mysql_query("use preem",$link);
        mysql_query("set names 'utf8'",$link);
        return $link;
    }


}